package cn.wuyu.baselibrary.widget;

import android.app.Dialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.wang.avi.AVLoadingIndicatorView;
import com.wang.avi.indicators.LineSpinFadeLoaderIndicator;

import java.util.Objects;

import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import cn.wuyu.baselibrary.R;


/**
 * 加载对话框（目前已添加到WuYuBaseActivity）
 * TODO 后面添加到请求
 * Created by king on 2017/9/28.
 */

public class WuYuLoadingDialog extends DialogFragment {
    private final String TAG = "WuYuLoadingDialog.TAG";

    private TextView tvMessage;

    private  String message;
    private AVLoadingIndicatorView avLoadingIndicatorView;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        View view = inflater.inflate(R.layout.dialog_loading, null);// 得到加载view
        tvMessage = view.findViewById(R.id.dialog_loading_message);
        tvMessage.setText(message);
        avLoadingIndicatorView = view.findViewById(R.id.AVLoadingIndicatorView);
        avLoadingIndicatorView.setIndicator(new LineSpinFadeLoaderIndicator());
        Dialog loadingDialog = new Dialog(Objects.requireNonNull(getActivity()), R.style.black_loading_dialog);// 创建自定义样式dialog
        loadingDialog.setCanceledOnTouchOutside(false);
        loadingDialog.setCancelable(false);// 不可以用“返回键”取消
        loadingDialog.setContentView(view);// 设置布局
        loadingDialog.setOnKeyListener((dialog, keyCode, event) -> {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                return true;
            }
            return false;
        });
        return loadingDialog;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public   void startAnim(){
        if (avLoadingIndicatorView!=null){
            avLoadingIndicatorView.show();
        }
    }

    public   void stopAnim(){
        if (avLoadingIndicatorView!=null){
            avLoadingIndicatorView.hide();
        }
    }
}
